﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace ControlsTest
{
    public partial class ControlsForm : Form
    {
        // Valid characters for category names: a->z, A->Z, 0->9, space and .-$@!{}()[]
        private Regex reg = new Regex(@"^[a-zA-Z0-9 \.\-\$\@\!\{\}\(\)\[\]]{0,}$");

        public ControlsForm()
        {
            InitializeComponent();
        }

        private void comboBox1_Leave(object sender, EventArgs e)
        {
            if (!reg.IsMatch(comboBox1.Text))
            {
                comboBox1.Focus();
                MessageBox.Show(@"You have used illegal characters in the category name. In general, the category name must use only characters that are valid in a Windows file name: A-Z, a-z, 0-9, space, and the characters: .-$@!{}()[]",
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btnShowForm1_Click(object sender, EventArgs e)
        {
            Form1 f1 = new Form1();
            f1.ShowDialog();
        }

        private void btnShowForm2_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.ShowDialog();
        }

        private void btnShowForm3_Click(object sender, EventArgs e)
        {
            Form3 f3 = new Form3();
            f3.ShowDialog();
        }
        private string invalidCharacters = @"[^a-zA-Z0-9 \.\-\$\@\!\{\}\(\)\[\]]";
        private void ultraCombo1_TextChanged(object sender, EventArgs e)
        {
            if (!reg.IsMatch(ultraCombo1.Text))
            {
                int oldLength = ultraCombo1.Text.Length;
                int currentPos = ultraCombo1.SelectionStart;
                ultraCombo1.Text = Regex.Replace(ultraCombo1.Text, invalidCharacters, "");
                int newLength = ultraCombo1.Text.Length;
                MessageBox.Show(@"You have used illegal characters in the category name. In general, the category name must use only characters that are valid in a Windows file name: A-Z, a-z, 0-9, space, and the characters: .-$@!{}()[]",
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //ultraCombo1.Select(currentPos - (oldLength - newLength), 0);
                ultraCombo1.SelectionStart = currentPos - (oldLength - newLength);
            }
        }

        private void ControlsForm_Load(object sender, EventArgs e)
        {
            ultraComboEditor1.Items.Add("a");
            ultraComboEditor1.Items.Add("ab");
            ultraComboEditor1.Items.Add("ac");
        }

        private void ultraComboEditor1_TextChanged(object sender, EventArgs e)
        {
            if (!reg.IsMatch(ultraComboEditor1.Text))
            {
                int oldLength = ultraComboEditor1.Text.Length;
                int currentPos = ultraComboEditor1.SelectionStart;
                ultraComboEditor1.Text = Regex.Replace(ultraComboEditor1.Text, invalidCharacters, "");
                int newLength = ultraComboEditor1.Text.Length;
                ultraComboEditor1.Select(currentPos - (oldLength - newLength), 0);
                //ultraComboEditor1.SelectionStart = currentPos - (oldLength - newLength);
                MessageBox.Show(@"You have used illegal characters in the category name. In general, the category name must use only characters that are valid in a Windows file name: A-Z, a-z, 0-9, space, and the characters: .-$@!{}()[]",
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void comboBox2_TextChanged(object sender, EventArgs e)
        {
            //if (!reg.IsMatch(comboBox2.Text))
            //{
            //    int oldLength = comboBox2.Text.Length;
            //    int currentPos = comboBox2.SelectionStart;
            //    comboBox2.Text = Regex.Replace(comboBox2.Text, invalidCharacters, "");
            //    int newLength = comboBox2.Text.Length;
            //    MessageBox.Show(@"You have used illegal characters in the category name. In general, the category name must use only characters that are valid in a Windows file name: A-Z, a-z, 0-9, space, and the characters: .-$@!{}()[]",
            //        "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    comboBox2.Select(currentPos - (oldLength - newLength), 0);
            //}

        }

        private void comboBox2_TextUpdate(object sender, EventArgs e)
        {
            if (!reg.IsMatch(comboBox2.Text))
            {
                int oldLength = comboBox2.Text.Length;
                int currentPos = comboBox2.SelectionStart;
                comboBox2.Text = Regex.Replace(comboBox2.Text, invalidCharacters, "");
                int newLength = comboBox2.Text.Length;
                MessageBox.Show(@"You have used illegal characters in the category name. In general, the category name must use only characters that are valid in a Windows file name: A-Z, a-z, 0-9, space, and the characters: .-$@!{}()[]",
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                comboBox2.Select(currentPos - (oldLength - newLength), 0);
            }

        }
    }
}
